#include <bits/stdc++.h>

using namespace std;

int r, s;
char matrix[25][25];
bool vis[26]; // 26 letters A-Z
int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int max_count = 0;

void search(int x, int y, int count) {
    max_count = max(max_count, count); // Update max_count with the current count

    for (int i = 0; i < 4; i++) {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];

        // Check bounds and if the letter has not been visited
        if (nx >= 0 && nx < r && ny >= 0 && ny < s && !vis[matrix[nx][ny] - 'A']) {
            vis[matrix[nx][ny] - 'A'] = true; // Mark the letter as visited
            search(nx, ny, count + 1); // Move to the next cell
            vis[matrix[nx][ny] - 'A'] = false; // Backtrack
        }
    }
}

int main() {
    cin >> r >> s;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < s; j++) {
            cin >> matrix[i][j];
        }
    }

    vis[matrix[0][0] - 'A'] = true; // Mark the starting letter as visited
    search(0, 0, 1); // Start searching from the top-left corner
    cout << max_count << endl; // Output the maximum count of unique letters
    return 0;
}